<?php
namespace app\message\validate;
use app\validate\Common;
use think\Validate;
use think\Db;
class Message extends Common
{
	/*验证规则*/
	protected $rule = [
		'message_name'			=> 'require|message_name',
	];
	/*验证提示信息*/
	protected $message = [
		'message_name.require'		=> '模板名称不能为空',
		'message_name.message_name'	=> '模板名称已存在',
	];
	/*验证场景*/
	protected $scene = [
		'ajax'	=> ['message_name'],
	];

	/**
	 * 自定义方法
	 */
	protected function message_name($value, $rule, $data)
	{
		$where['type'] = $data['type'];
		if(isset($data['message_id'])){
			$where['message_id'] = ['neq',$data['message_id']];
		}
		$count = Db::name('message')
		->where('message_name',$value)
		->where($where)
		->count();
		return $count > 0 ? false : true;
	}
}